-- story_ending.lua - 修仙游戏结局相关故事

return {
    -- 返回村庄节点
    {
        id = "return_to_village",
        title = "返回村庄",
        text = function(state)
            local quest_completed = state.variables["game.quest_completed"]
            local boars_killed = state.variables["game.boars_killed"] or 0
            
            if quest_completed then
                return [[你成功完成了任务，村长和村民们都对你表示感谢。
'你是我们村的英雄！'
村里为你举办了一场盛大的庆祝宴会。]]
            elseif boars_killed > 0 then
                return string.format([[你返回了村庄，告诉村长你杀死了%d只野猪。
村长对你的努力表示肯定，但希望你能继续完成任务。]], boars_killed)
            else
                return [[你空手而归，村长对你有些失望。
'希望你能再试一次，村里真的需要帮助。']]
            end
        end,
        vars = function(state)
            local quest_completed = state.variables["game.quest_completed"]
            if quest_completed then
                return {
                    ["player.gold"] = function(state)
                        return (state.variables["player.gold"] or 0) + 50
                    end,
                    ["game.village_hero"] = true,
                    ["game.score"] = function(state)
                        return (state.variables["game.score"] or 0) + 30
                    end
                }
            end
            return {}
        end,
        choices = {
            {
                text = "继续修炼",
                next = "start_cultivation"
            },
            {
                text = "前往青玄宗",
                next = "go_to_qingxuan"
            },
            {
                text = "留在村里守护大家",
                next = "stay_in_village"
            }
        }
    },
    
    -- 前往青玄宗节点
    {
        id = "go_to_qingxuan",
        title = "前往青玄宗",
        text = [[你决定前往青玄宗，寻求更高的修仙之道。
经过几天的跋涉，你终于来到了青玄宗的山脚下。
山门上写着四个大字：'青玄圣地'。]],
        vars = {
            ["game.location"] = "qingxuan_mountain",
            ["game.score"] = function(state)
                return (state.variables["game.score"] or 0) + 20
            end
        },
        choices = {
            {
                text = "上山拜师",
                next = "apply_to_sect"
            },
            {
                text = "在山脚下修炼",
                next = "cultivate_at_foot"
            },
            {
                text = "返回村庄",
                next = "village_outside"
            }
        }
    },
    
    -- 留在村庄节点
    {
        id = "stay_in_village",
        title = "守护村庄",
        text = [[你决定留在村里，守护着村民们。
在你的保护下，村庄变得更加繁荣。
你成为了村里的守护者，受到所有人的尊敬。]],
        vars = {
            ["game.ending"] = "village_guardian",
            ["game.score"] = function(state)
                return (state.variables["game.score"] or 0) + 40
            end
        },
        choices = {}
    },
    
    -- 加入门派节点
    {
        id = "apply_to_sect",
        title = "加入青玄宗",
        text = function(state)
            local cultivation = state.variables["player.cultivation"]
            local score = state.variables["game.score"] or 0
            
            if cultivation == "凡人" then
                return [[你尝试加入青玄宗，但因为没有修为而被拒绝了。
'回去修炼几年再来吧。']]
            elseif cultivation == "练气一层" or cultivation == "练气二层" then
                return [[你通过了青玄宗的入门测试，成为了一名外门弟子。
'好好修炼，将来有机会成为内门弟子。']]
            elseif score >= 150 then
                return [[你凭借着强大的修为和出色的表现，直接被青玄宗的长老收为亲传弟子。
'你很有天赋，将来必成大器。']]
            else
                return [[你通过了青玄宗的测试，成为了一名内门弟子。
'继续努力，宗门的未来就靠你们了。']]
            end
        end,
        vars = function(state)
            local cultivation = state.variables["player.cultivation"]
            local score = state.variables["game.score"] or 0
            local result = {
                ["game.score"] = function(state)
                    return (state.variables["game.score"] or 0) + 30
                end
            }
            
            if cultivation == "凡人" then
                result["game.ending"] = "rejected_by_sect"
            elseif cultivation == "练气一层" or cultivation == "练气二层" then
                result["game.ending"] = "outer_disciple"
            elseif score >= 150 then
                result["game.ending"] = "personal_disciple"
            else
                result["game.ending"] = "inner_disciple"
            end
            
            return result
        end,
        choices = {}
    },
    
    -- 一代宗师结局
    {
        id = "become_grandmaster",
        title = "一代宗师",
        text = function(state)
            local name = state.variables["player.name"] or "叶辰"
            return string.format([[经过数十年的修炼，你成为了一代宗师。
你的名字%s响彻整个修仙界。
你开创了自己的门派，培养了无数优秀的弟子。
最终，你突破了凡人的极限，飞升成仙。]], name)
        end,
        vars = {
            ["game.ending"] = "grandmaster",
            ["game.score"] = function(state)
                return (state.variables["game.score"] or 0) + 100
            end
        },
        choices = {}
    },
    
    -- 平凡结局
    {
        id = "ordinary_ending",
        title = "平凡的一生",
        text = function(state)
            local name = state.variables["player.name"] or "叶辰"
            return string.format([[你选择了平凡的生活，在村里度过了一生。
虽然没有成为强大的修仙者，但你过得很幸福。
你的故事被村里的孩子们代代相传。]], name)
        end,
        vars = {
            ["game.ending"] = "ordinary",
            ["game.score"] = function(state)
                return (state.variables["game.score"] or 0) + 20
            end
        },
        choices = {}
    },
    
    -- 悲剧结局
    {
        id = "tragic_ending",
        title = "悲剧结局",
        text = [[你在一次冒险中遇到了强大的敌人，最终不敌。
你的修仙之路就此结束，但你的精神将永远激励着后人。
'修仙之路充满荆棘，只有真正的勇者才能走到最后。']],
        vars = {
            ["game.ending"] = "tragic",
            ["game.score"] = function(state)
                return (state.variables["game.score"] or 0) + 10
            end
        },
        choices = {}
    },
    
    -- 完美结局
    {
        id = "perfect_ending",
        title = "完美结局",
        text = function(state)
            local name = state.variables["player.name"] or "叶辰"
            return string.format([[你不仅成为了强大的修仙者，还守护了自己的家乡。
你建立了一个和谐的修仙门派，让更多的人有机会踏上修仙之路。
最终，你带着自己的亲友一起飞升成仙，成就了一段传奇。
你的故事将永远被人们传颂。]], name)
        end,
        vars = {
            ["game.ending"] = "perfect",
            ["game.score"] = function(state)
                return (state.variables["game.score"] or 0) + 150
            end
        },
        choices = {}
    }
}